<!-- 课程评价管理 -->
<template>
  <div class='app-container'>
    <el-col :span="24" :xs="24">
      <el-form :model="tableParams" ref="queryForm" size="small" :inline="true" v-show="true" label-width="100px">
        <el-form-item label="用户名称" prop="userName">
          <el-input v-model="tableParams.userName" placeholder="请输入用户名称" clearable style="width: 240px"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="评价内容" prop="commentContent">
          <el-input v-model="tableParams.commentContent" placeholder="请输入评价内容" clearable style="width: 240px"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <!-- <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['system:user:add']">新增</el-button>
        </el-col> -->
        <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar> -->
      </el-row>
      <my-table :loading="loading" rowKey="id" :tableData="tableData"  :params="tableParams">
        <el-table-column prop="userName" label="用户名称" align="center">
        </el-table-column>
        <el-table-column prop="commentLevel" label="评价星级" align="center"></el-table-column>
        <el-table-column prop="commentContent" label="评价内容" align="center"></el-table-column>
        <el-table-column prop="createTime" label="评价时间" align="center">
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handleEdit(scope.row)"
              v-hasPermi="['system:user:edit']">编辑</el-button>
            <el-button type="text" size="mini" @click="handleDelete(scope.row)"
              v-hasPermi="['system:user:remove']">删除</el-button>
          </template>
        </el-table-column>
      </my-table>
    </el-col>
    <!-- 课程评价弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" width="600px" append-to-body
      :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :model="dialogForm" :rules="rules" ref="dialogForm" size="small" label-width="120px">
        <el-form-item label="评价星级" prop="commentLevel">
          <el-rate allow-half v-model="dialogForm.commentLevel"></el-rate>
        </el-form-item>
        <el-form-item label="评价内容" prop="commentContent">
          <el-input type="textarea" v-model="dialogForm.commentContent" placeholder="请输入评价内容" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { getCourseRateList,updateCourseRate} from '@/api/courseManage/courseRate';
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      loading: false,
      tableData: [],//表格数据
      tableParams: {
        courseId:this.$route.query.courseId,
        userName: "",
        commentContent: "",
        pageNum: 1,
        pageSize: 10,
      },//表格参数
      dialogFormVisible: false,//弹窗显示隐藏
      dialogForm: {
        commentLevel: 0,
        commentContent: "",
        courseCommentId:"",
        courseId:this.$route.query.courseId,
      },//弹窗数据
      dialogTitle: '编辑课程评价',
      rules: {
        commentLevel: [
          { required: true, message: '请选择评价星级', trigger: 'blur' },
        ],
        commentContent: [
          { required: true, message: '请输入评价内容', trigger: 'blur' },
        ],
      },
      submiting: false,
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    //获取表格数据
    getList() {
      this.loading = true;
      getCourseRateList(this.tableParams).then(res => {
        this.loading = false;
        if (res.code == 200) {
          this.tableData = res.rows;
        } else {
          this.$message.error(res.msg)
        }
      }).catch(err=>{
        this.loading =false;
      })
    },
    // 重置
    resetQuery() {
      this.tableData = [];
      this.tableParams.userName = "";
      this.tableParams.commentContent = "";
      this.tableParams.pageNum = 1;
      this.getList();
    },
    //修改
    handleEdit(row) {
      console.log(row);
      this.dialogFormVisible = true;
      this.dialogTitle = '编辑课程评价';
      this.dialogForm = {
        commentLevel: row.commentLevel,
        commentContent: row.commentContent,
        courseCommentId:row.courseCommentId,
      }
    },
    //提交
    submitForm() {
      console.log(this.dialogForm);
      this.$refs['dialogForm'].validate((valid) => {
        if (valid) {
          this.submiting = true;
          let params = {
            commentLevel: this.dialogForm.commentLevel,
            commentContent: this.dialogForm.commentContent,
            courseCommentId:this.dialogForm.courseCommentId,
            courseId:this.dialogForm.courseId,
          }
          updateCourseRate(params).then(res => {
            this.submiting = false;
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.dialogFormVisible = false;
              this.getList();
            } else {
              this.$message.error(res.msg)
            }
          }).catch(err => {
            this.submiting = false;
          })
        } else {
          return false;
        }
      });
    },
    // 删除
    handleDelete(row) {
      this.$confirm('是否确认删除该课程评价？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCourseType(row.id).then(res => {
          console.log(res);
          if (res.code == 200) {
            this.$message.success(res.msg);
            this.getList();
          } else {
            this.$message.error(res.msg)
          }
        }).catch(err => {
          console.log(err);
        })
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    },
    cancel() {
      this.dialogFormVisible = false;
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {

  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.getList();
  },
  beforeCreate() { }, //生命周期 - 创建之前
  beforeMount() { }, //生命周期 - 挂载之前
}
</script>
<style scoped></style>
